<?php

namespace interface\v1;
/*
*待检验钢瓶
*/

class inspectSteelCylinderClass extends index
{
    private const page = 'pastSteelCylinder'; # 权限
    private const table = 'steel_cylinder'; # 数据表

    public function search()
    {
        $this->code         = 1;
        $this->resp['data'] = [
            'orderNo' => '物料条码',
        ];
    }

    /**
     * 详情
     *
     * @return void
     */
    public function detail(): void
    {
        $where = ' 1=1 ';
        $and   = search([
            '='    => [
                'coid'    => self::$coid,
                'barcode' => $this->post('barcode'),
                'type_cylinder' => 2,
                'state' => $this->post('state'),
            ],
            'like' => [

            ],
        ]);
        if (self::isNull($and)) {
            $this->resp['warn'] = '条件不能为空';
        } else {
            $page = $this->page(["table" => self::table, "where" => $where.$and,'order'=>'time desc']);
            $warehouseTr = [];
            foreach ($page['array'] as $array) {
                $warehouseTr[] = [
                    'id'       => $array['id'],
                    'barcode'       => $array['barcode'],
                    'code'        => $array['code'],
                    'nextTime'    =>  date("Y-m-d H:i:s",strtotime($array['lastTime'] . ' +2 year')),
                ];
            }
            $this->resp['data'] = [
                'warehouse' => $warehouseTr,
                'sql' => $page['sql'],
            ];
            $this->code         = 1;
        }
    }

    /**
     * 检验
     * @return bool|string
     */
    public function edit()
    {
        $id = $this->post('id');
        $state = $this->post('state');
        $info = query(self::table,"id = '" . $id . "' and status = '".$state."'");
        if (!$info) {
            $this->resp['warn'] = '条件不能为空';
        } else {
            update(["table" => self::table , "field" => [
                'state' => '质检中',
            ], "where" => "id = '" . $id . "'"]);
            static::insert([
                "table" =>  self::table.'_log',
                "field" => [
                    'id'                 => self::getId(),
                    'steelCylinderId'    => $id,
                    'coid'               => self::$coid,
                    'barcode'            => $info['barcode'],
                    'code'               => $info['code'],
                    'model'              => $info['model'],
                    'text'               => '',
                    'state'              => '待检验',
                    'stid'               => self::$stid,
                    'updateTime'         => $this->time,
                    'time'               => $this->time,
                ]]);
        }
        $this->code         = 1;
        $this->resp['warn'] = '操作成功';
        $this->resp['data'] = [
            'barcode' => $info['barcode']
        ];
    }
}